package Presetacion;

import Negocio.AAsterisco;
import Negocio.Escalada2;
import Negocio.EscaladaSimple;
import Negocio.Estado;
import Negocio.MetodoBusqueda;
import Negocio.PrimeroAnchura;
import Negocio.PrimeroEnProfundidadDer;
import Negocio.PrimeroMejor;
import Run.VentanaDetalle;
import java.awt.Dimension;
import java.io.File;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Test.java
 *
 * Created on 07-oct-2011, 17:58:29
 */
/**
 *
 * @author Rodrigo
 */
public class Test extends javax.swing.JFrame {

    private MetodoBusqueda metodo;
    private File imagen;
    private Thread hiloOrdenar;
    private Estado save;
    private boolean procesando = false;
    private boolean ordenando = false;
    private boolean ordenado = false;
    private int intervalo=0;

    /** Creates new form Test */
    public Test() {
        initComponents();

        cmbMetodo.addItem(new PrimeroMejor());
        cmbMetodo.addItem(new AAsterisco());
        cmbMetodo.addItem(new PrimeroAnchura());
        cmbMetodo.addItem(new PrimeroEnProfundidadDer());
        cmbMetodo.addItem(new EscaladaSimple());
        cmbMetodo.addItem(new Escalada2());
        cmbMetodo.setSelectedIndex(0);        
  
        validarBotones();
        btnDesordenar.setVisible(false);
    }

    @Override
    public void pack() {
        this.setMinimumSize(this.getPreferredSize());
        super.pack();        
    }

    public void validarBotones() {
        boolean ocioso=(procesando==false&&ordenando==false);
        btnCargar.setEnabled((save == null) ? false : true);       
        btnDesordenar.setEnabled(ocioso&&tb.isIniciado()==true);
        btnDetener.setEnabled(procesando);
        btnTerminar.setEnabled(ordenando);
        btnOrdenar.setEnabled(ocioso&&tb.isIniciado()==true); 
        cmbMetodo.setEnabled(btnOrdenar.isEnabled());
        btnDetalle.setEnabled(ordenado);
        btnIniciar.setEnabled(ocioso);
        btnGuardar.setEnabled(tb.isIniciado());
    }

    public void setOrdenando(boolean ordenando) {
        this.ordenando = ordenando;
        if(ordenando=true)        
            this.procesando=this.ordenado =false;       
        validarBotones();
    }
    
    public void setOrdenado(boolean ordenado) {
        this.ordenado = ordenado;
        if(ordenado=true)        
            this.procesando=this.ordenando=false;     
        validarBotones();
    }

    public void setProcesando(boolean procesando) {
        this.procesando = procesando;
         if(procesando=true)
            this.ordenando =this.ordenado=false;
         validarBotones();
    }    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tb = new Presetacion.Tablero();
        jPanel1 = new javax.swing.JPanel();
        txtFila = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtCol = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        txtImagen = new javax.swing.JTextField();
        chkImagen = new javax.swing.JCheckBox();
        btnExaminar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        btnDesordenar = new javax.swing.JButton();
        btnIniciar = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        btnCargar = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        cmbMetodo = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        btnDetalle = new javax.swing.JButton();
        btnOrdenar = new javax.swing.JButton();
        pnlAvence = new javax.swing.JPanel();
        prgBar = new javax.swing.JProgressBar();
        btnDetener = new javax.swing.JButton();
        btnTerminar = new javax.swing.JButton();
        pnlMetricas = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        txtMovimientos = new javax.swing.JTextField();
        txtTiempo = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tb.setBorder(javax.swing.BorderFactory.createTitledBorder("Puzzle"));

        javax.swing.GroupLayout tbLayout = new javax.swing.GroupLayout(tb);
        tb.setLayout(tbLayout);
        tbLayout.setHorizontalGroup(
            tbLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 854, Short.MAX_VALUE)
        );
        tbLayout.setVerticalGroup(
            tbLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 293, Short.MAX_VALUE)
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Configuración"));

        txtFila.setText("3");

        jLabel3.setText("Columnas:");

        jLabel2.setText("Filas:");

        txtCol.setText("3");

        txtImagen.setEditable(false);

        chkImagen.setText("Imagen (opcional):");
        chkImagen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkImagenActionPerformed(evt);
            }
        });

        btnExaminar.setText("Examinar");
        btnExaminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExaminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(chkImagen)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtImagen, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnExaminar))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(txtImagen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(chkImagen)
                .addComponent(btnExaminar))
        );

        btnDesordenar.setText("Desordenar");
        btnDesordenar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDesordenarActionPerformed(evt);
            }
        });

        btnIniciar.setText("Iniciar");
        btnIniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIniciarActionPerformed(evt);
            }
        });

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCargar.setText("Cargar");
        btnCargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCargarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtFila, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtCol, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(206, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(btnCargar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnGuardar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 74, Short.MAX_VALUE)
                .addComponent(btnIniciar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnDesordenar))
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtFila, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(txtCol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDesordenar)
                    .addComponent(btnIniciar)
                    .addComponent(btnGuardar)
                    .addComponent(btnCargar))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Ordenamiento"));

        cmbMetodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbMetodoActionPerformed(evt);
            }
        });

        jLabel4.setText("Metodo:");

        btnDetalle.setText("Detalle");
        btnDetalle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDetalleActionPerformed(evt);
            }
        });

        btnOrdenar.setText("Ordenar");
        btnOrdenar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOrdenarActionPerformed(evt);
            }
        });

        pnlAvence.setBorder(javax.swing.BorderFactory.createTitledBorder("Avance"));

        btnDetener.setText("Detener");
        btnDetener.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDetenerActionPerformed(evt);
            }
        });

        btnTerminar.setText("+ Velocidad");
        btnTerminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTerminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlAvenceLayout = new javax.swing.GroupLayout(pnlAvence);
        pnlAvence.setLayout(pnlAvenceLayout);
        pnlAvenceLayout.setHorizontalGroup(
            pnlAvenceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(prgBar, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlAvenceLayout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addComponent(btnTerminar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDetener))
        );
        pnlAvenceLayout.setVerticalGroup(
            pnlAvenceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAvenceLayout.createSequentialGroup()
                .addComponent(prgBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlAvenceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDetener)
                    .addComponent(btnTerminar)))
        );

        pnlMetricas.setBorder(javax.swing.BorderFactory.createTitledBorder("Metricas"));

        jLabel6.setText("Movimientos:");

        txtMovimientos.setEditable(false);

        txtTiempo.setEditable(false);

        jLabel7.setText("Tiempo (mili seg):");

        javax.swing.GroupLayout pnlMetricasLayout = new javax.swing.GroupLayout(pnlMetricas);
        pnlMetricas.setLayout(pnlMetricasLayout);
        pnlMetricasLayout.setHorizontalGroup(
            pnlMetricasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMetricasLayout.createSequentialGroup()
                .addGroup(pnlMetricasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlMetricasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtTiempo, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtMovimientos, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        pnlMetricasLayout.setVerticalGroup(
            pnlMetricasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMetricasLayout.createSequentialGroup()
                .addGroup(pnlMetricasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtMovimientos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlMetricasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtTiempo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbMetodo, 0, 243, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnOrdenar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDetalle))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addComponent(pnlAvence, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlMetricas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDetalle)
                    .addComponent(btnOrdenar)
                    .addComponent(jLabel4)
                    .addComponent(cmbMetodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(pnlMetricas, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlAvence, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tb, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tb, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnIniciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIniciarActionPerformed
        // TODO add your handling code here:
        int fila=3,columna=3;
        
        if (chkImagen.isSelected() == true && imagen == null) {
            JOptionPane.showMessageDialog(this, "Elija una imagen valida", "Error", JOptionPane.ERROR_MESSAGE);
             return;
        }
        
        try{
            fila=Integer.parseInt(txtFila.getText());
            columna=Integer.parseInt(txtCol.getText());
            if(fila<1||columna<1)
                throw new RuntimeException();
        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(this, "Los números de filas y columanas son incorectos.\nDeben ser mayores a 1.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        tb.iniciar(fila,columna , (chkImagen.isSelected()) ? imagen.getAbsolutePath() : "");
        save=null;
        validarBotones();
        this.pack();
//       int[][] estado=new int[][]{{0,1,2},{3,4,5},{6,8,7}};
//       tb.setEstado(new Estado(estado));
//        

    }//GEN-LAST:event_btnIniciarActionPerformed

    private void btnDesordenarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDesordenarActionPerformed
        // TODO add your handling code here:
        tb.desordenar(1000);

    }//GEN-LAST:event_btnDesordenarActionPerformed

    private void cmbMetodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbMetodoActionPerformed
        // TODO add your handling code here:
        metodo = (MetodoBusqueda) cmbMetodo.getSelectedItem();
        btnDetalle.setEnabled(false);
    }//GEN-LAST:event_cmbMetodoActionPerformed

    private void btnDetalleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDetalleActionPerformed
        // TODO add your handling code here:
        VentanaDetalle prin = new VentanaDetalle(this, true, metodo.getArbol(), tb);
        prin.setVisible(true);

    }//GEN-LAST:event_btnDetalleActionPerformed

    private void btnOrdenarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOrdenarActionPerformed
        // TODO add your handling code here:


        hiloOrdenar = new Thread() {

            @Override
            public void run() {
                tb.setEnabled(false);
                txtTiempo.setText("");
                txtMovimientos.setText("");                
//                btnDetener.setEnabled(true);
//                btnOrdenar.setEnabled(false);

                prgBar.setString("Procesando...");
                prgBar.setIndeterminate(true);
                prgBar.setStringPainted(true);
                prgBar.setValue(0);

                metodo.setEstadoInicial(tb.getEstado());
                metodo.setEstadoFinal(tb.getEstadoFinal());

                Long tiempo = System.currentTimeMillis();
                setProcesando(true);                
                metodo.procesar();
                tiempo = System.currentTimeMillis() - tiempo;
//        tiempo = (tiempo / 1000);

                btnDetalle.setEnabled(true);
                final List<Estado> optimo = metodo.resolucionAutomatica();

                txtTiempo.setText(tiempo + "");
                txtMovimientos.setText(optimo.size() - 1 + "");                

                setOrdenando(true);
                prgBar.setString("Ordenando...");
                prgBar.setIndeterminate(false);
                prgBar.setMinimum(0);
                prgBar.setMaximum(optimo.size());                
                
                try {
                    intervalo=500;
                    for (int i = 0; i < optimo.size(); i++) {
                        Thread.currentThread().sleep(intervalo);
                        tb.setEstado(optimo.get(i));
                        prgBar.setValue(i+1);
                    }
                } catch (Exception exx) {
                }
                setOrdenado(true);                
                prgBar.setString("Terminado");
                tb.setEnabled(true);
            }
        };
        hiloOrdenar.start();

    }//GEN-LAST:event_btnOrdenarActionPerformed

    private void btnExaminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExaminarActionPerformed
        // TODO add your handling code here:
        try {
            JFileChooser choser = new JFileChooser(imagen);
            choser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            if (choser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
                ImageIO.read(choser.getSelectedFile());
                imagen = choser.getSelectedFile();
                txtImagen.setText(imagen.getName());
            }
        } catch (Exception exe) {
            JOptionPane.showMessageDialog(this, "Archivo invalido", "Error", JOptionPane.ERROR_MESSAGE);
        }


    }//GEN-LAST:event_btnExaminarActionPerformed

    private void chkImagenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkImagenActionPerformed
        // TODO add your handling code here:
        btnExaminar.setEnabled(chkImagen.isSelected());

    }//GEN-LAST:event_chkImagenActionPerformed

    private void btnDetenerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDetenerActionPerformed
        // TODO add your handling code here:
        hiloOrdenar.stop();        
        prgBar.setIndeterminate(false);         
        prgBar.setStringPainted(false);
        setProcesando(false);
        tb.setEnabled(true);
        validarBotones();
    }//GEN-LAST:event_btnDetenerActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        save = tb.getEstado();
        validarBotones();
        //tb.setEstado(save);
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnCargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCargarActionPerformed
        // TODO add your handling code here:
        //tb.setEstado(new Estado(new int[][]{{2,4,0},{5,1,3},{7,6,8}}));
        tb.setEstado(save);
        validarBotones();
    }//GEN-LAST:event_btnCargarActionPerformed

    private void btnTerminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTerminarActionPerformed
        // TODO add your handling code here:
        intervalo=intervalo/2;       
    }//GEN-LAST:event_btnTerminarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Test().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCargar;
    private javax.swing.JButton btnDesordenar;
    private javax.swing.JButton btnDetalle;
    private javax.swing.JButton btnDetener;
    private javax.swing.JButton btnExaminar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnIniciar;
    private javax.swing.JButton btnOrdenar;
    private javax.swing.JButton btnTerminar;
    private javax.swing.JCheckBox chkImagen;
    private javax.swing.JComboBox cmbMetodo;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JPanel pnlAvence;
    private javax.swing.JPanel pnlMetricas;
    private javax.swing.JProgressBar prgBar;
    private Presetacion.Tablero tb;
    private javax.swing.JTextField txtCol;
    private javax.swing.JTextField txtFila;
    private javax.swing.JTextField txtImagen;
    private javax.swing.JTextField txtMovimientos;
    private javax.swing.JTextField txtTiempo;
    // End of variables declaration//GEN-END:variables
}
